package com.py.credit.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.common.ServiceException;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.Template;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.entity.enums.ResourceTemplateFieldsEnum;
import com.py.credit.service.TemplateService;
import com.py.credit.utils.MapCache;
import com.py.framework.core.common.Page;

@RestController
@RequestMapping(value = "/template")
public class TemplateController {

	@Autowired
	private TemplateService templateService;

	private final static String FRONT_TABLE_ID = "template";

	/**
	 * 获取初始化列
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getOptionalColums.json")
	public List<OptionalColumn> getOptionalColums() {

		return ResourceTemplateFieldsEnum.getOptionalColumns();
	}

	@RequestMapping(value = "/getAdvancedConditionColumns.json")
	public List<OptionalColumn> getAdvancedConditionColumns() {

		return ResourceTemplateFieldsEnum.getAdvancedConditionColumns();

	}

	/**
	 * 初始化数据
	 * 
	 * @param condition
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/list.json")
	public RestServiceResult<List<Map<String, Object>>> getTableList(
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page) {

		String[] configColumns = (String[]) MapCache.get(1000 + "_" + FRONT_TABLE_ID);

		List<String> showColumns = (configColumns == null || configColumns.length == 0)
				? ResourceTemplateFieldsEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
		// condition="[{columnEN:\"batch_id\",value:\"EX-test-1-20180326-1174\",type:\"varchar\",fuzzy:fasle}]";
		String[] additionalColumns = { "id" };

		List<Map<String, Object>> tableList = templateService.getByConditionForPage(showColumns, additionalColumns,
				condition, page);

		return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);

	}

	/**
	 * 手动选择展示列
	 * 
	 * @param columns
	 * @return
	 */
	@RequestMapping(value = "/addShowColunms.json")
	public RestServiceResult<String> addShowColunms(
			@RequestParam(value = "columns", required = true) String[] columns) {
		MapCache.set(1000 + "_" + FRONT_TABLE_ID, columns);
		return new RestServiceResult<String>();
	}

	/**
	 * 获取模板相关的字段
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectMbCode.json")
	public RestServiceResult<List<Map<String, Object>>> selectMbCode() {
		List<Map<String, Object>> template = templateService.selectMbCode();
		return new RestServiceResult<List<Map<String, Object>>>(template);
	}

	/**
	 * 添加模板
	 * 
	 * @param name
	 * @param status
	 * @param content
	 * @param start_time
	 * @param end_time
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/add.json")
	public RestServiceResult<Object> add(@RequestParam(defaultValue = "", required = true, value = "name") String name,
			@RequestParam(defaultValue = "", required = true, value = "content") String content,
			@RequestParam(defaultValue = "", required = true, value = "type") String type,
			@RequestParam(defaultValue = "", required = true, value = "start_time") String start_time,
			@RequestParam(defaultValue = "", required = true, value = "end_time") String end_time) {
		int managerId = 1;
		// 判断数据库中是否存在相同名称的公告
		int num = templateService.selectSameName(null, name);
		if (num > 0) {
			throw new ServiceException("存在相同名称的模板");
		} else {
			templateService.addTemplate(name, content, type, managerId, start_time, end_time);
			return new RestServiceResult<Object>();
		}
	}

	/**
	 * 启用功能
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/startEnable.json")
	public RestServiceResult<Object> startEnable(
			@RequestParam(defaultValue = "", required = true, value = "id") int id) {
		Template template = templateService.selectOne(id);
		int num = templateService.selectSameType(template.getType());
		if (num > 1) {
			throw new ServiceException("同一种模板类型只能启用一个");
		} else {
			templateService.startEnable(id);
			templateService.insertTemplateData(id, template.getStart_time(), template.getEnd_time());
			return new RestServiceResult<Object>();
		}
	}

	/**
	 * 查询单条数据
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/selectOne.json")
	public RestServiceResult<Object> selectOne(@RequestParam(defaultValue = "", required = true, value = "id") int id) {
		Template template = templateService.selectOne(id);
		return new RestServiceResult<Object>(template);
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/del.json")
	public RestServiceResult<Object> del(@RequestParam(defaultValue = "", required = true, value = "ids") String ids) {
		templateService.del(ids);
		return new RestServiceResult<Object>();
	}

	/**
	 * 修改
	 * 
	 * @param id
	 * @param name
	 * @param status
	 * @param content
	 * @param start_time
	 * @param end_time
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/modify.json")
	public RestServiceResult<Object> modify(@RequestParam(defaultValue = "", required = true, value = "id") String id,
			@RequestParam(defaultValue = "", required = true, value = "name") String name,
			@RequestParam(defaultValue = "", required = true, value = "content") String content,
			@RequestParam(defaultValue = "", required = true, value = "start_time") String start_time,
			@RequestParam(defaultValue = "", required = true, value = "end_time") String end_time) {

		// 判断数据库中是否存在其他相同名称的模板
		int num = templateService.selectSameName(id, name);

		if (num > 0) {
			throw new ServiceException("存在相同名称的模板");
		}

		templateService.modify(id, name, content, start_time, end_time);
		return new RestServiceResult<Object>();
	}

	/**
	 * 预览
	 * 
	 * @param text
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/preview.json")
	public RestServiceResult<Object> preview(
			@RequestParam(defaultValue = "", required = true, value = "text") String text) {
		String text1 = text;
		List<String> list = new ArrayList<String>();
		String rgex = "(?<=\\{\\{)(.*?)(?=\\}\\})";
		Pattern p = Pattern.compile(rgex);
		Matcher m = p.matcher(text);
		while (m.find()) {
			list.add(m.group());
		}
		for (String in : list) {
			String aString = "{{" + in + "}}";
			text1 = text1.replace(aString, "XXX");
		}
		return new RestServiceResult<Object>(text1);
	}

}
